package com.gxuwz.endrskyweather;

import android.content.Intent;
import android.graphics.LinearGradient;
import android.graphics.Shader;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextPaint;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.gxuwz.endrskyweather.viewmodel.AppInfoViewModel;

public class AppInfoFragment extends Fragment {

    private AppInfoViewModel viewModel;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_app_info, container, false);
        viewModel = new ViewModelProvider(this).get(AppInfoViewModel.class);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        
        TextView titleView = view.findViewById(R.id.textViewTitle);
        
        // 定义渐变色的颜色数组
        int[] colors = { 0xFF9EFBD3, 0xFF57E9F2, 0xFF45D4FB };
        
        // 获取 TextView 的文字画笔
        TextPaint textPaint = titleView.getPaint();
        
        // 设置渐变色的 Shader
        int width = titleView.getWidth();
        if (width == 0) {
            // 如果宽为 0，则使用默认宽
            width = (int) titleView.getPaint().measureText(titleView.getText().toString(), 
                    0, titleView.getText().length());
        }
        Shader shader = new LinearGradient(
                0, 0, width, titleView.getTextSize(),  // 从左到右的渐变
                colors, null, Shader.TileMode.CLAMP
        );
        textPaint.setShader(shader);
        
        // 设置开源仓库按钮点击事件
        view.findViewById(R.id.btn_to_gitee).setOnClickListener(v -> {
            try {
                String repoUrl = "https://gitee.com/SPGLP/rsky-weather-app";
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(repoUrl));
                startActivity(browserIntent);
                Log.d("AppInfoFragment", "打开开源仓库: " + repoUrl);
            } catch (Exception e) {
                Toast.makeText(getContext(), "无法打开浏览器", Toast.LENGTH_SHORT).show();
                Log.e("AppInfoFragment", "打开开源仓库失败", e);
            }
        });

        // 设置检查更新按钮点击事件
        view.findViewById(R.id.btn_check_update).setOnClickListener(v -> {
            try {
                // 此处解耦不完全，不符合 MVVM 规范，日后优化
                viewModel.doCheckUpdate(requireActivity());
                Log.d("AppInfoFragment", "检查更新被调用");
            } catch (Exception e) {
                Toast.makeText(getContext(), "系统错误：检查更新失败", Toast.LENGTH_SHORT).show();
                Log.e("AppInfoFragment", "检查更新失败", e);
            }
        });

        // 设置 ViewModel 监听器
        viewModel.getResultCode().observe(getViewLifecycleOwner(), code -> {
            if (code != null) {
                if (code == 0) {
                    Toast.makeText(getContext(), "发现新版本，开始下载", Toast.LENGTH_SHORT).show();
                    Log.d("AppInfoFragment", "发现新版本，开始下载");
                }
                if (code == 1) {
                    Toast.makeText(getContext(), "检查更新失败，请检查网络", Toast.LENGTH_SHORT).show();
                    Log.d("AppInfoFragment", "检查更新失败，请检查网络");
                }
                if (code == 2) {
                    Toast.makeText(getContext(), "当前已是最新版本", Toast.LENGTH_SHORT).show();
                    Log.d("AppInfoFragment", "当前已是最新版本");
                }
            }
        });

    }
}
